//
//  FXSeachView.swift
//  微博(Swift)
//
//  Created by V on 15/12/27.
//  Copyright © 2015年 V. All rights reserved.
//

import UIKit

class FXSeachView: UIView {
    // text
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var textFieldRightConstraint: NSLayoutConstraint!
    // 取消按钮
    @IBOutlet weak var cancelButton: UIButton!
    
    
    
    
    class func seachView() ->FXSeachView {
        return NSBundle.mainBundle().loadNibNamed("seachView", owner: nil, options: nil).last! as!
        FXSeachView
    }

    
    
    
    @IBAction func textFieldEdiitingBegin(sender: AnyObject) {
        UIView.animateWithDuration(1) { () -> Void in
            // 闭包里边使用 外部变量 需要使用 self
            self.textFieldRightConstraint.constant = self.cancelButton.bounds.width
            // 执行约束动画
            // 1. 改约束
            // 2. 在动画的代码块里面执行 self.layoutIfNeeded()
            self.layoutIfNeeded()
        }
    }
    
    
    // 取消
    @IBAction func cancelButtonAction(sender: AnyObject) {
        // 1. 更改约束
        textFieldRightConstraint.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
        // 2. 取消第一响应
        textField.resignFirstResponder()
        
    }
    
    
    
    // 左边视图
    // awakeFromNib 与 initWithCoder 这两个方法是与 View 从 xib 里面加载才会调用的方法
    override func awakeFromNib() {
        // xib 完全转成一个 view
        // xib 里面的子控件连线完成
        let leftImgView = UIImageView()
        leftImgView.image = UIImage(named: "searchbar_textfield_search_icon")
        // leftImgView.sizeToFit()
        leftImgView.frame.size = CGSizeMake(textField.frame.height, textField.frame.height)
        leftImgView.contentMode = .Center
        textField.leftView = leftImgView
        // 设置leftView的 模式
        textField.leftViewMode = .Always
        
        // 边框
        // textField.layer.borderWidth = 1
        // textField.layer.borderColor = UIColor.cyanColor().CGColor
        // textField.layer.cornerRadius = 15
        // textField.cornerRadius = 15
        
    }
    
    
}
